<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 13-12-3
 * Time: 下午9:09
 */

class ComboboxWidget extends Widget
{
    /**
     * 执行处理
     * @param $params.table 表名
     * @param $params.condition 条件
     * @param $params.fields 字段
     * @param $params.order 排序
     * @param $params.id 对象ID
     * @param $params.name 对象名称
     * @param $params.style 对象样式
     *
     * @return string
     */
    public function render($params)
    {
        //模型
        if(empty($params['table'])) return;
        $db = D(ucfirst($params['table']));

        if(method_exists($db, 'combobox')){
            $params['data'] = $db->combobox($params);
        }else{
            //条件查询
            $params['data'] = $db->where($params['condition'])
                ->field($params['fields'])
                ->order($params['order'])
                ->select();
            if($params['idField'] && $params['valField']){
                foreach($params['data'] as $key => $row){
                    $row['id'] = $row[$params['idField']];
                    $row['name'] = $row[$params['valField']];
                    $params['data'][$key] = $row;
                }
            }
        }
        $tpl = isset($params['tpl']) ? $params['tpl'] : 'combobox';
        $content = $this->renderFile($tpl, $params);
        return $content;
    }
}